from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
import os, re

# Import the decorator
from template import RendersTemplate
# Because decorators are usually functions and functions are not CamelCase
renders_template = RendersTemplate

_MATCHER = re.compile(r'([A-Z])')

def discover(*view_modules):
  """ Given a list of modules, returns a URL mapping for WSGIApplication
  
  Assumes a convention where the CamelCase names of RequestHandlers refer
  to URL paths. For example, /root/index points to RootIndex
  
  """
  mapping = {}
  
  for module in view_modules:
    for name in dir(module):
      if name.startswith('_'):
        continue
      value = getattr(module, name)      
      try:
        if issubclass(value, webapp.RequestHandler):
          path = _MATCHER.sub(r'/\1', name).lower()
          mapping[path] = value
        
      except TypeError: # issubclass throws TypeError if first arg not class
        pass
  
  return mapping.items()
